package com.qf.dao.impl;

import com.qf.dao.AdminDao;
import com.qf.pojo.Admin;
import com.qf.pojo.User;
import com.qf.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Kuke
 * @date 2022/3/2 16:12
 * 针对管理数据访问接口实现
 */
public class AdminDaoImpl implements AdminDao {

    /**
     * 通过管理员名称查询管理员
     * @param name 管理员名称
     * @return 返回的管理实体
     */
    @Override
    public Admin selectAdminByName(String name) throws SQLException {

        //创建执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from admin where name = ? " ;
        //执行查询
        Admin admin = qr.query(sql, new BeanHandler<Admin>(Admin.class), name);
        return admin;
    }

    /**
     * 查询所有普通用户
     * @return 返回用户列表
     */
    @Override
    public List<User> selectAllUser() throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        String sql = "select * from user" ;
        List<User> list = qr.query(sql, new BeanListHandler<User>(User.class));
        return list;
    }

    /**
     * 数据访问:通过用户uid删除用户
     * @param uid 用户的id号
     */
    @Override
    public void deleteUserByUid(String uid) throws SQLException {

        //创建执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;

        //sql
        String sql = "delete  from user where uid = ?" ;
        //执行update操作
        int count = qr.update(sql, uid);
        System.out.println(count);
    }

    /**
     * 管理员查询用户,通过用户id获取用户
     * @param uid  用户编号
     * @return 返回用户实体对象
     */
    @Override
    public User selectUserByUid(String uid) throws SQLException {
        //创建执行对
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where uid = ?" ;
        //查询
        User user = qr.query(sql, new BeanHandler<User>(User.class), uid);
        return user;
    }

    /**
     * 修改用户
     * @param user 用户实体
     */
    @Override
    public void updateUserByUid(User user) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "update user set username = ?,password=?,name=?,email=?,birthday=? where uid = ?" ;
        int count = qr.update(sql,
                user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getEmail(),
                user.getBirthday()
                , user.getUid());
        System.out.println(count);
    }

    /**
     * 分页查询用户列表数据
     * @param curentPage 当前页码
     * @param pageSize   每页显示的条数
     * @return  返回分页列表数据
     */
    @Override
    public List<User> selectUserByPage(int curentPage, int pageSize) throws SQLException {
        //创建执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user limit ?,?" ;
        //执行查询
        List<User> list = qr.query(sql, new BeanListHandler<User>(User.class), (curentPage - 1) * pageSize, pageSize);

        return  list ;
    }

    /**
     * 查询总记录数
     * @return 实际总条数
     */
    @Override
    public int selectTotalCount() throws SQLException {

        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;

        //sql
        String sql  = "select count(*) from  user " ;
        //执行
        Object obj = qr.query(sql, new ScalarHandler<>());
        String str = String.valueOf(obj);
        int totalCount = Integer.parseInt(str);
        return totalCount;
    }
}
